package com.unitd.filesystem.utils;

import com.unitd.filesystem.comm.FileType;

/**
 * @desc 处理互联网上文件路径信息的工具类
 * @filename HttpFilePathHelper.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2017/3/6
 */
public class HttpFilePathHelper {

	public static final String HTTP_PREFIX = "http://";
	public static final String HTTPS_PREFIX = "https://";

	/**
	 * @desc 根据文件URL获取对应的文件类型信息(文件后缀名)
	 * @param filePath 文件URl(http请求URL)
	 * @return FileType
	 */
	public static FileType parseFileType(String filePath){
		if(filePath.contains("/")){
			filePath = filePath.substring(filePath.lastIndexOf("/"));
		}
		filePath = filePath.split("\\?")[0];
		if(filePath.contains(".")){
			String suffix = filePath.substring(filePath.lastIndexOf(".") + 1);
			return FileType.getFileTypeBySuffix(suffix);
		}
		return null;
	}

	public static void main(String[] args) {
		System.out.println(parseFileType("http:www.ssss.com/cccc/123.png?xxx"));
		System.out.println(parseFileType("123.png"));
		System.out.println(parseFileType("http:www.ssss.com/cccc/dtgh4r4tt/"));
		System.out.println("=================");
		String str = ".jpg";
		System.out.println(FileType.getFileTypeBySuffix(str));
	}
}